// database/migrations/2025112301_create_plugin_data_tables.js
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建插件数据批次表
    await queryInterface.createTable('plugin_data_batches', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      batch_id: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '批次ID'
      },
      data_type: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '数据类型（bank/order/invoice）'
      },
      count: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '数据条数'
      },
      status: {
        type: Sequelize.STRING(20),
        allowNull: false,
        defaultValue: 'pending',
        comment: '处理状态（pending/processing/completed/failed）'
      },
      processed_count: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '已处理条数'
      },
      failed_count: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '失败条数'
      },
      processing_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '处理耗时（毫秒）'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '更新时间'
      },
      completed_at: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '完成时间'
      },
      error_message: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '错误信息'
      }
    }, {
      comment: '插件数据批次表'
    });
    
    // 创建插件数据项表
    await queryInterface.createTable('plugin_data_items', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      item_id: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '数据项ID'
      },
      batch_id: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '批次ID'
      },
      url: {
        type: Sequelize.TEXT,
        allowNull: false,
        comment: '请求URL'
      },
      method: {
        type: Sequelize.STRING(10),
        allowNull: false,
        comment: 'HTTP方法'
      },
      category: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '数据分类（bank/order/invoice）'
      },
      status: {
        type: Sequelize.STRING(20),
        allowNull: false,
        defaultValue: 'pending',
        comment: '处理状态（pending/processed/failed）'
      },
      request_data: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '请求数据'
      },
      response_data: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '响应数据'
      },
      rule_info: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '规则信息'
      },
      processed_at: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '处理时间'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '更新时间'
      },
      error_message: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '错误信息'
      }
    }, {
      comment: '插件数据项表'
    });
    
    // 为插件数据项表创建索引
    await queryInterface.addIndex('plugin_data_items', ['batch_id']);
    await queryInterface.addIndex('plugin_data_items', ['category']);
    await queryInterface.addIndex('plugin_data_items', ['status']);
  },
  
  down: async (queryInterface, Sequelize) => {
    // 删除表
    await queryInterface.dropTable('plugin_data_items');
    await queryInterface.dropTable('plugin_data_batches');
  }
};